
package com.hulk.dryad.persistence.injector.datascope;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hulk.dryad.common.constant.SecurityConstants;
import com.hulk.dryad.common.util.SecurityUtils;
import com.hulk.dryad.persistence.entity.SysDeptRelationModel;
import com.hulk.dryad.persistence.entity.SysRoleModel;
import com.hulk.dryad.persistence.service.SysDeptRelationService;
import com.hulk.dryad.persistence.service.SysRoleService;
import com.hulk.dryad.persistence.admin.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.GrantedAuthority;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kim
 * @date 2019-09-07
 * <p>
 * 默认data scope 判断处理器
 */
public class DryadDatascopeHandle implements DataScopeHandle {

	@Lazy
	@Autowired
	private SysRoleService sysRoleService;
	@Lazy
	@Autowired
	private SysDeptRelationService relationService;

	/**
	 * 计算用户数据权限
	 * @param deptList
	 * @return
	 */
	@Override
	public Boolean calcScope(List<Integer> deptList) {
		AdminUser user = SecurityUtils.getUser();
		List<Integer> roleIdList = user.getAuthorities().stream().map(GrantedAuthority::getAuthority)
				.filter(authority -> authority.startsWith(SecurityConstants.ROLE))
				.filter(authority-> (!authority.startsWith(SecurityConstants.ROLE_ACTIVITI)))
				.map(authority -> Integer.valueOf(authority.split(StrUtil.UNDERLINE)[1])).collect(Collectors.toList());
		// 当前用户的角色为空
		if (CollectionUtil.isEmpty(roleIdList)) {
			return false;
		}
		SysRoleModel role = sysRoleService.findRolesByRoleIds(roleIdList, CollUtil.join(roleIdList, StrUtil.UNDERLINE)).stream()
				.min(Comparator.comparingInt(SysRoleModel::getDsType)).orElse(null);
		// 角色有可能已经删除了
		if (role == null) {
			return false;
		}
		Integer dsType = role.getDsType();
		// 查询全部
		if (DataScopeTypeEnum.ALL.getType() == dsType) {
			return true;
		}
		// 自定义
		if (DataScopeTypeEnum.CUSTOM.getType() == dsType) {
			String dsScope = role.getDsScope();
			deptList.addAll(
					Arrays.stream(dsScope.split(StrUtil.COMMA)).map(Integer::parseInt).collect(Collectors.toList()));
		}
		// 查询本级及其下级
		if (DataScopeTypeEnum.OWN_CHILD_LEVEL.getType() == dsType) {
			LambdaQueryWrapper<SysDeptRelationModel> qw = Wrappers.<SysDeptRelationModel>lambdaQuery().eq(SysDeptRelationModel::getAncestor, user.getDeptId());
			List<Integer> deptIdList = relationService.list(qw).stream()
					.map(SysDeptRelationModel::getDescendant).collect(Collectors.toList());
			deptList.addAll(deptIdList);
		}
		// 只查询本级
		if (DataScopeTypeEnum.OWN_LEVEL.getType() == dsType) {
			deptList.add(user.getDeptId());
		}
		return false;
	}

}
